package com.yjhb.android.sbz.fragment.taskmanage;

import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.ManagerCenterActivity;
import com.yjhb.android.sbz.activity.RefreshPackageActivity;
import com.yjhb.android.sbz.adapter.taskmanage.TaskManagerDisplayAdapter;
import com.yjhb.android.sbz.dialog.InputDialog;
import com.yjhb.android.sbz.dialog.MainPageToppingDialog;
import com.yjhb.android.sbz.dialog.TipsDialog;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.ManagerTask;
import com.yjhb.android.sbz.enums.TaskState;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.tool.CommonDialogCollect;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;

/**
 * 创建者 zx
 * 创建时间 2020/11/20 17:32
 */
public class SuspendFragment extends BaseTaskManageFragment<TaskManagerDisplayAdapter> {
    private final String TAG = "SuspendFragment";

    @Override
    public CharSequence getTitle() {
        return TaskState.TASK_STATE_STOP.getStateStr();
    }

    @Override
    protected TaskManagerDisplayAdapter getAdapter() {
        return new TaskManagerDisplayAdapter(getContext());
    }


    @Override
    protected Integer getState() {
        return TaskState.TASK_STATE_STOP.getState();
    }

    @Override
    public void onItemClick(int index, Object o) {
        super.onItemClick(index, o);
        Intent intent = new Intent(getContext(), ManagerCenterActivity.class);
        intent.putExtra("state", getState());
        intent.putExtra("taskId", dataSet.get(index).getTask().getId());
        startActivity(intent);
    }

    @Override
    public void onViewClick(View view, String msg, int index, Object o) {
        super.onViewClick(view, msg, index, o);
        ManagerTask managerTask = (ManagerTask) o;
        if (msg.equals("check")) {
            Tool.startCheckListActivity(getContext(),managerTask);
        } else if (msg.equals("top")) {
            final MainPageToppingDialog dialog = new MainPageToppingDialog((Activity) getContext());
            dialog.setOnClickListener((dialog1, button) -> {
                dialog.dismiss();
                if (button == BaseDialog.BUTTON_SUBMIT) {
                    Integer hour = dialog.getHour();
                    if (hour == null) {
                        ZxToast.showINFOBottomToast("请输入正确的时间");
                        return;
                    }
                    presenter.showMainPage(managerTask.getTask().getId() + "", hour);
                }
            });
            dialog.show();
        } else if (msg.equals("refresh")) {
            final TipsDialog tipsDialog = CommonDialogCollect.taskRefresh(getContext());
            tipsDialog.setOnClickListener(type -> {
                tipsDialog.dismiss();
                if (type == TipsDialog.TYPE_SUBMIT)
                    presenter.refreshTask(managerTask.getTask().getId() + "");
            });
            tipsDialog.show();
        } else if (msg.equals("redpack")) {
            Tool.startSendRedPackActivity(getContext(), managerTask.getTask().getId());
        } else if (msg.equals("addmoney")) {
            final InputDialog inputDialog = CommonDialogCollect.addMoney(getContext());
            inputDialog.setOnClickListener(new InputDialog.OnClickListener() {
                @Override
                public void onClick(int button) {
                    if (button == InputDialog.BUTTON_CANCEL) {
                        inputDialog.dismiss();
                    } else if (button == InputDialog.BUTTON_SUBMIT) {
                        EditText editText = inputDialog.getInputView();
                        if (Tool.isEditEmpty(editText)) {
                            ZxToast.showBottomToastLight("请输入金额");
                            return;
                        }
                        BigDecimal money = new BigDecimal(editText.getText().toString());
                        if (money.floatValue() < 0.2f) {
                            ZxToast.showINFOBottomToast("请至少提高0.2元");
                            return;
                        }
                        presenter.addMoney(managerTask.getTask().getId() + "", money);
                        inputDialog.dismiss();
                    }
                }
            });
            inputDialog.show();
        } else if (msg.equals("count")) {
            final InputDialog inputDialog = CommonDialogCollect.addNum(getContext());
            inputDialog.setOnClickListener(new InputDialog.OnClickListener() {
                @Override
                public void onClick(int button) {
                    if (button == InputDialog.BUTTON_CANCEL) {
                        inputDialog.dismiss();
                    } else if (button == InputDialog.BUTTON_SUBMIT) {
                        EditText editText = inputDialog.getInputView();
                        if (Tool.isEditEmpty(editText)) {
                            ZxToast.showBottomToastLight("请输入数量");
                            return;
                        }
                        int count = Integer.parseInt(editText.getText().toString());
                        if (count < 5) {
                            ZxToast.showINFOBottomToast("请至少增加5个名额");
                            return;
                        }
                        presenter.addTaskCount(managerTask.getTask().getId() + "", count);
                        inputDialog.dismiss();
                    }
                }
            });
            inputDialog.show();
        } else if (msg.equals("fast")) {
            TipsDialog tipsDialog = CommonDialogCollect.quickTrial(getContext(), managerTask);
            tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
                @Override
                public void onClick(int type) {
                    tipsDialog.dismiss();
                    if (type == TipsDialog.TYPE_SUBMIT) {
                        presenter.fastCheck(managerTask.getTask().getId() + "",
                                managerTask.getTask().getAuditFast() == 0);
                    }
                }
            });
            tipsDialog.show();
        } else if (msg.equals("stop")) {
            final TipsDialog tipsDialog = new TipsDialog(getContext());
            tipsDialog.setTitle("提示");
            boolean isStop = managerTask.getTask().getState() != TaskState.TASK_STATE_STOP.getState();
            if (isStop)
                tipsDialog.setContent("任务暂停后将不再对外展示。\n请确定是否暂停？");
            else {
                tipsDialog.setContent("任务恢复后将对外展示。\n请确定是否恢复？");
            }
            tipsDialog.setContentGravity(Gravity.CENTER);
            tipsDialog.setSubmitText("确定" + (isStop ? "暂停" : "恢复"));
            tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
                @Override
                public void onClick(int type) {
                    if (type == TipsDialog.TYPE_SUBMIT) {
                        presenter.stopOrRestartTask(managerTask.getTask().getId() + "", isStop);
                    }
                    tipsDialog.dismiss();
                }
            });
            tipsDialog.show();
        }else if (msg.equals("refresh_package")) {
            startActivity(new Intent(getContext(), RefreshPackageActivity.class));
        } else if (msg.equals("timer_recommend")) {
            Tool.startAddTimerActivity(getContext(), managerTask.getTask().getId(), 1);
        } else if (msg.equals("auto_refresh")) {
            Tool.startAddTimerActivity(getContext(), managerTask.getTask().getId(), 2);
        }
    }

    @Override
    public void onSuccess(String tips, String taskId) {
        super.onSuccess(tips, taskId);
        if (ResManager.getString(R.string.task_tips_stop_success).equals(tips) || ResManager.getString(R.string.task_tips_restart_success).equals(tips)){
            EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_UPDATE_DATA_TASK_MANAGER_LIST, TaskState.TASK_STATE_STOP));
            EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_UPDATE_DATA_TASK_MANAGER_LIST, TaskState.TASK_STATE_DISPLAY));
        }
    }

    @Override
    protected CharSequence getTips() {
        return null;
    }
}
